package com.uls.web.controller.questionnaire;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.uls.common.core.domain.entity.SysUser;




import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.questionnaire.domain.Response;
import com.uls.questionnaire.service.IResponseService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 存储回答信息的Controller
 * 
 * @author zxh
 * @date 2024-11-17
 */
@RestController
@RequestMapping("/questionnaire/response")
public class ResponseController extends BaseController
{
    @Autowired
    private IResponseService responseService;

    /**
     * 查询存储回答信息的列表
     */
    @PreAuthorize("@ss.hasPermi('questionnaire:response:list')")
    @GetMapping("/list")
    public TableDataInfo list(Response response)
    {
        startPage();
        List<Response> list = responseService.selectResponseList(response);
        return getDataTable(list);
    }

    /**
     * 导出存储回答信息的列表
     */
    @PreAuthorize("@ss.hasPermi('questionnaire:response:export')")
    @Log(title = "存储回答信息的", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Response responses)
    {
        List<Response> list = responseService.selectResponseList(responses);
        ExcelUtil<Response> util = new ExcelUtil<Response>(Response.class);
        util.exportExcel(response, list, "存储回答信息的数据");
    }

    /**
     * 获取存储回答信息的详细信息
     */
    @PreAuthorize("@ss.hasPermi('questionnaire:response:query')")
    @GetMapping(value = "/{responseId}")
    public AjaxResult getInfo(@PathVariable("responseId") Long responseId)
    {
        return success(responseService.selectResponseByResponseId(responseId));
    }

    /**
     * 新增存储回答信息的
     */
    @PreAuthorize("@ss.hasPermi('questionnaire:response:add')")
    @Log(title = "存储回答信息的", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Response response)
    {
        return toAjax(responseService.insertResponse(response));
    }


    /**
     * 修改存储回答信息的
     */
    @PreAuthorize("@ss.hasPermi('questionnaire:response:edit')")
    @Log(title = "存储回答信息的", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Response response)
    {
        return toAjax(responseService.updateResponse(response));
    }

    /**
     * 删除存储回答信息的
     */
    @PreAuthorize("@ss.hasPermi('questionnaire:response:remove')")
    @Log(title = "存储回答信息的", businessType = BusinessType.DELETE)
	@DeleteMapping("/{responseIds}")
    public AjaxResult remove(@PathVariable Long[] responseIds)
    {
        return toAjax(responseService.deleteResponseByResponseIds(responseIds));
    }



    @PostMapping("/submitBatch")
    public String submitResponsesBatch(@RequestBody List<Response> responses) {
    SysUser user = this.getLoginUser().getUser();
    // 为每个 Response 添加创建人和提交时间

    for (Response response : responses) {
        if(response.getIsanonymous()==0)
        {
            response.setResponseBy(" ");
            response.setResponseTime(new Date());
        }
        else {
            response.setResponseBy(user.getUserName());
            System.out.println(user.getUserName());
            response.setResponseTime(new Date());
        }// 设置统一的提交时间
    }


    responseService.saveResponsesBatch(responses);

    return "Responses submitted successfully";
}

}
